package org.jeecg.modules.param.advise.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.MemberAPI;
import org.jeecg.common.api.dto.MemberDTO;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.modules.param.advise.entity.BikeMemberUserAdvise;
import org.jeecg.modules.param.advise.mapper.BikeMemberUserAdviseMapper;
import org.jeecg.modules.param.advise.service.IBikeMemberUserAdviseService;
import org.jeecg.modules.utils.BikeResult;
import org.jeecg.modules.utils.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Comparator;
import java.util.List;

/**
 * @Description: 用户意见反馈
 * @Author: 月月鸟
 * @Date:   2021-06-01
 * @Version: V1.0
 */
@Service
public class BikeMemberUserAdviseServiceImpl extends ServiceImpl<BikeMemberUserAdviseMapper, BikeMemberUserAdvise> implements IBikeMemberUserAdviseService {
    @Resource
    private BikeMemberUserAdviseMapper bikeMemberUserAdviseMapper;

    @Autowired
    private MemberAPI memberAPI;

    @Value("${application.domain}")
    public String domain;

    public String getDomain() {
        return domain;
    }

    public void setDomain(String domain) {
        this.domain = domain;
    }

    @Override
    public Result<?> pageList(BikeMemberUserAdvise bikeMemberUserAdvise, Integer pageNo, Integer pageSize) {
        List<BikeMemberUserAdvise> bikeMemberUserAdvises = bikeMemberUserAdviseMapper.pageList(bikeMemberUserAdvise);
        //添加图片路径前缀
        for (int i = bikeMemberUserAdvises.size()-1; i>=0 ; i--){
            BikeMemberUserAdvise memberUserAdvise = bikeMemberUserAdvises.get(i);
            if (StringUtils.isNotEmpty(memberUserAdvise.getFileUrlOne())){
                memberUserAdvise.setFileUrlOne(domain+memberUserAdvise.getFileUrlOne());
            }
            if (StringUtils.isNotEmpty(memberUserAdvise.getFileUrlTwo())){
                memberUserAdvise.setFileUrlTwo(domain+memberUserAdvise.getFileUrlTwo());
            }
            if (StringUtils.isNotEmpty(memberUserAdvise.getFileUrlThree())){
                memberUserAdvise.setFileUrlThree(domain+memberUserAdvise.getFileUrlThree());
            }
        }
        bikeMemberUserAdvises.sort(new Comparator<BikeMemberUserAdvise>() {
            @Override
            public int compare(BikeMemberUserAdvise o1, BikeMemberUserAdvise o2) {
                return o2.getCreateTime().compareTo(o1.getCreateTime());
            }
        });
        return Result.OK(bikeMemberUserAdvises);
    }

    @Override
    public ModelAndView exportXls(HttpServletRequest request, BikeMemberUserAdvise bikeMemberUserAdvise) {
        List<BikeMemberUserAdvise> bikeMemberUserAdvises = bikeMemberUserAdviseMapper.pageList(bikeMemberUserAdvise);
        //添加图片路径前缀
        for (int i = bikeMemberUserAdvises.size()-1; i>=0 ; i--){
            BikeMemberUserAdvise memberUserAdvise = bikeMemberUserAdvises.get(i);
            if (StringUtils.isNotEmpty(memberUserAdvise.getFileUrlOne())){
                memberUserAdvise.setFileUrlOne(domain+memberUserAdvise.getFileUrlOne());
            }
            if (StringUtils.isNotEmpty(memberUserAdvise.getFileUrlTwo())){
                memberUserAdvise.setFileUrlTwo(domain+memberUserAdvise.getFileUrlTwo());
            }
            if (StringUtils.isNotEmpty(memberUserAdvise.getFileUrlThree())){
                memberUserAdvise.setFileUrlThree(domain+memberUserAdvise.getFileUrlThree());
            }
        }
        bikeMemberUserAdvises.sort(new Comparator<BikeMemberUserAdvise>() {
            @Override
            public int compare(BikeMemberUserAdvise o1, BikeMemberUserAdvise o2) {
                return o2.getCreateTime().compareTo(o1.getCreateTime());
            }
        });
        return ExcelUtils.exportXls(request,bikeMemberUserAdvises,BikeMemberUserAdvise.class,"用户意见反馈");
    }

    @Override
    public BikeResult getAdviseList(Integer pageNo, Integer pageSize,HttpServletRequest request) {
        String accessToken = request.getHeader("X-Access-Token");
        // 解密获得username，用于和数据库进行对比
        String username = JwtUtil.getUsername(accessToken);
        MemberDTO m = memberAPI.getMember(username);

        Page<BikeMemberUserAdvise> page= new Page(pageNo,pageSize);
        Page<BikeMemberUserAdvise> userList = bikeMemberUserAdviseMapper.selectPage(page, new QueryWrapper<BikeMemberUserAdvise>().eq("user_id", m.getId()));

        userList.getRecords().forEach(
                vs->{
                    if (StringUtils.isNotEmpty(vs.getFileUrlOne())){
                        vs.setFileUrlOne(domain+vs.getFileUrlOne());
                    }
                    if (StringUtils.isNotEmpty(vs.getFileUrlThree())){
                        vs.setFileUrlThree(domain+vs.getFileUrlThree());
                    }
                    if (StringUtils.isNotEmpty(vs.getFileUrlTwo())){
                        vs.setFileUrlTwo(domain+vs.getFileUrlTwo());
                    }
                }
        );


        return BikeResult.ok("1","获取意见反馈列表成功",userList);
    }
}
